package com.sea.demo.shiro.service.impl;

import com.github.pagehelper.PageHelper;
import com.sea.demo.shiro.common.ShiroUtils;
import com.sea.demo.shiro.domain.User;
import com.sea.demo.shiro.mapper.UserMapper;
import com.sea.demo.shiro.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Author: 快乐在角落里
 * @Create: 2020/11/8 2:17
 */
@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User selectUserByUsername(String username) {
        return userMapper.selectUserByUsername(username);
    }

    @Override
    public Set<String> selectRoleCodeSet(Integer id) {
        return userMapper.selectRoleCodeSet(id);
    }

    @Override
    public Set<String> selectPermissionCodeSet(Integer id) {
        return userMapper.selectPermissionCodeSet(id);
    }

    @Override
    public int insert(User user) {
        String salt = ShiroUtils.generateSalt();
        String password = ShiroUtils.generatePassword(user.getPassword(), salt);
        user.setPassword(password);
        user.setSalt(salt);
        return userMapper.insert(user);
    }

    @Override
    public int delete(Integer id) {
        return userMapper.delete(id);
    }

    @Override
    public int update(User user) {
        return userMapper.update(user);
    }

    @Override
    public List<User> select(User user) {
        return select(user,null,null);
    }

    @Override
    public List<User> select(User record, Integer currentPage, Integer pageSize) {
        return select(record,currentPage,pageSize,null);
    }

    @Override
    public List<User> select(User user, Integer currentPage, Integer pageSize, String orderBy) {
        PageHelper.startPage(currentPage,pageSize,orderBy);
        return userMapper.select(user);
    }

    @Override
    public User selectOne(Integer id) {
        return userMapper.selectOne(id);
    }
    
}
